//
//  SearchPatient.m
//  Cabinet
//
//  Created by Florent Montsegur on 01/05/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SearchPatient.h"


@implementation SearchPatient

@synthesize mPersons;
@synthesize mCountLabel;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        mTableControler = [[PersonViewControler alloc] init];
    }
    
    return self;
}

- (NSString*)windowNibName
{
	return @"SearchPerson";
}


- (void)withParentWindow:parentWindow
{
    mSelectedIndex = -1;
	NSWindow* window = [self window];
	
	[NSApp beginSheet:window modalForWindow:parentWindow modalDelegate:nil didEndSelector:nil contextInfo:nil];
	[NSApp runModalForWindow:window];
	// sheet is up now...
	
	[NSApp endSheet:window];
	[window orderOut:self];
}


- (void)windowDidLoad
{
    [super windowDidLoad];
    
    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
    
    [mPersons setDataSource:mTableControler];
    
    [[self mCountLabel] setStringValue:[NSString stringWithFormat:@"%ld personne(s)", [mTableControler count]]];
}

- (void) closeWindow
{
    [mPersons setDataSource:nil];
    [[self window] orderOut:nil];
    [NSApp stopModal];
}

- (Person*) selectedPerson
{
    return [mTableControler getPersonAtIndex:mSelectedIndex];
}

- (IBAction)okAction:sender {
    
    NSLog(@"On OK");
    
    mSelectedIndex = [[self mPersons] selectedRow];
    
    mResult = NSOKButton;
    [self closeWindow];
}

- (IBAction)annulerAction:sender {
    
    NSLog(@"On Annuler");
    
    mResult = NSCancelButton;
    [self closeWindow];
}

- (int) returnCode
{
    return mResult;
}

- (void) setPersons:(NSArray*)aPersonList
{
    [mTableControler setPersons:aPersonList];
    [mPersons reloadData];
}

@end
